package BusinessLogic;

import java.util.ArrayList;

import Data.GoodData2;
import Data.GoodDataOperator;
import Data.LibraryOutRecordOperator;
import Data.UserDataOperator;

public class OutLibraryModel implements IOutLibraryModel{

	private GoodDataOperator gdo = new GoodDataOperator();
	private UserDataOperator udo = new UserDataOperator();
	private LibraryOutRecordOperator lor = new LibraryOutRecordOperator();
	private ArrayList<LibraryItem> outLibraryList = new ArrayList<LibraryItem>();
	
	public GoodDataOperator getGoodDataOperator(){
		return gdo;
	}
	
	public void setGoodDataOperator(GoodDataOperator gdope){
		gdo = gdope;
	}
	
	public UserDataOperator getUserDataOperator(){
		return udo;
	}
	
	public void setUserDataOperator(UserDataOperator udope){
		udo = udope;
	}
	
	public ArrayList<LibraryItem> getOutLibraryList(){
		return outLibraryList;
	}
	
	public void setOutLibraryList(ArrayList<LibraryItem> a){
		outLibraryList = a;
	}
	
	public boolean addItem(int id,String name,double price,double specialPrice,String specialStart,String specialEnd,double num,String produceDate,String scrapDate,double costPrice,String inLibraryDate){
		try{
			LibraryItem li = new LibraryItem(id,name,price,specialPrice,specialStart,specialEnd,num,produceDate,scrapDate,costPrice,inLibraryDate);
			outLibraryList.add(li);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	public boolean removeItem(int GoodID){
		try{
			for(LibraryItem li:outLibraryList){
				if(li.getID() == GoodID){
					outLibraryList.remove(li);
					break;
				}
			}
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	public boolean libraryOut(){
		for(LibraryItem li:outLibraryList){
			gdo.saveDataOutLibrary(li.getID(), li.getGoodName(), li.getPrice(), li.getSpecialPrice(), li.getSpecialStart(), li.getSpecialEnd(), li.getLibraryInNumber());
			
		}
		return true;
	}
	
	public LibraryItem showOutLibraryInformationByID(int ID){
		LibraryItem li = null;
		for(LibraryItem li2:outLibraryList){
			if(li2.getID() == ID){
				li = li2;
				break;
			}
			
		}
		return li;
	}
	
	public String getGoodDescriptionByID(int ID){
		LibraryItem li;
		li = showOutLibraryInformationByID(ID);
		return li.getGoodName();
	}
	
	public double getGoodPriceByID(int ID){
		LibraryItem li;
		li = showOutLibraryInformationByID(ID);
		return li.getPrice();
	}
	
	public double getGoodSpecialPriceByID(int ID){
		LibraryItem li;
		li = showOutLibraryInformationByID(ID);
		return li.getSpecialPrice();
	}
	
	public String getGoodSpecialTimeStartByID(int ID){
		LibraryItem li;
		li = showOutLibraryInformationByID(ID);
		return li.getSpecialStart();
	}
	
	public String getGoodSpecialTimeEndByID(int ID){
		LibraryItem li;
		li = showOutLibraryInformationByID(ID);
		return li.getSpecialEnd();
	}
	
	public double getGoodNumberByID(int ID){
		LibraryItem li;
		li = showOutLibraryInformationByID(ID);
		return li.getLibraryInNumber();
	}
	
	public String getGoodProduceDateByID(int ID){
		GoodData2 gd2 = gdo.readData2ByID(ID);
		return gd2.getProduceData();
	}
	public String getGoodScrapDateByID(int ID){
		GoodData2 gd2 = gdo.readData2ByID(ID);
		return gd2.getscrapDate();
	}
	public double getGoodCostPriceByID(int ID){
		GoodData2 gd2 = gdo.readData2ByID(ID);
		return gd2.getcostPrice();
	}

	public void saveLibraryOutRecord(int GoodID,double goodNum,String libraryOutPerson,String libraryOutDate,String libraryOutReason){
		lor.saveData(GoodID,goodNum,libraryOutPerson,libraryOutDate,libraryOutReason);
	}


}
